﻿/*!
*  CultuRAM - Agenda Cultural da R.A.M
*  by Valter Camacho
*/


/*
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
|                                   GLOBAL VARIABLES                                   |
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
*/

var registSelect = 0;
var selectedEventProp2x = false;


/*
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
|                                    DOCUMENT READY                                    |
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
*/

//Method to get Current page name from url. 
function GetCurrentPageName() {

    var pageUrl = document.location.href;
    var pageName = "";
    var result = pageUrl.split("/");

    if (result.length == 4) {
        pageName = result[3];
    }
    if (result.length == 5) {
        pageName = result[3] + "/" + result[4];
    }
    //var pageName = pageUrl.substring(pageUrl.lastIndexOf('/') + 1);     
    return pageName.toLowerCase() ;
}

$(document).ready(function () {
    
    //alert("menu!");
    $("#menu_adm>ul>li.active").removeClass("active");
    $("#menu_home>ul>li.active").removeClass("active");
    
    var currPage = GetCurrentPageName();
    //alert("Current Page: " + currPage);
    
    switch (currPage) {
        case '':
            $('#li_home').addClass('active');
            break;
        case 'home/about':
            $('#li_about').addClass('active');
            break;
        case 'home/accrequest':
            $('#li_accrequest').addClass('active');
            break;
        case 'home/accstatus':
            $('#li_accstatus').addClass('active');
            break;
        case 'home/contact':
            $('#li_contact').addClass('active');
            break;
        case 'home/faq':
            $('#li_faq').addClass('active');
            break;
        case 'home/auth':
            $('#li_auth').addClass('active');
            break;
        //case 'account/edit':
        //    $('#li_profile').addClass('active');
        //    break;

        //MENU_ADM
        case 'home':
            $('#li_home').addClass('active');
            break;
        case 'home/dashboard':
            $('#li_dashboard').addClass('active');
            break;
        case 'entity':
            $('#li_entities').addClass('active');
            break;
        case 'entity/create':
            $('#li_entities').addClass('active');
            break;
        case 'accreditingrequest':
            $('#li_entities').addClass('active');
            break;
        case 'event':
            $('#li_events').addClass('active');
            break;
        case 'event/create':
            $('#li_events').addClass('active');
            break;
        case 'account/usermanager':
            $('#li_users').addClass('active');
            break;
        case 'statistics':
            $('#li_statistics').addClass('active');
            break;
        case 'parameters':
            //alert("clicou nos Parameters");
            $('#li_parameters').addClass('active');
            break;
        case 'temp/help':
            $('#li_help').addClass('active');
            break;
        case 'temp/contactos':
            $('#li_contacts').addClass('active');
            break;
        case 'temp/alertas':
            $('#li_events').addClass('active');
            break;
        case 'temp/bilheteira':
            $('#li_events').addClass('active');
            break;
        case 'temp/destaques':
            $('#li_events').addClass('active');
            break;
        case 'temp/exportacao':
            $('#li_events').addClass('active');
            break;
    }

    //$("#txtDateStartEv").val("01/01/2014 0:00"); //PELO SISTEMA
    //$("#txtDateEndEv").val("31/12/2014 23:59"); //PELO SISTEMA
    
});


/*
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
|                                ACTIVE MENU NAVIGATION                                |
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
*/

$(function () {

    $('.navbar li a').on('click', function (e) {

        //e.preventDefault(); // prevent link click if necessary?

        var $thisLi = $(this).parent('li');
        var $ul = $thisLi.parent('ul');

        if (!$thisLi.hasClass('active')) {
            $ul.find('li.active').removeClass('active');
            $thisLi.addClass('active');
        }

    });

});


/*
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
|                               VALIDATION (Client-Side)                               |
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
*/

//Validacao das DropDownLists
$('.input-validation-error').closest(".control-group").addClass("error"); //Important for view: Home/AccRequest

$.validator.setDefaults({
    highlight: function (element) {
        //alert(element.type);
        $(element).closest(".control-group").addClass("error");
        $(element).closest(".editor-field").addClass("error");
        $(element).closest(".form-control").addClass("error");    
        $(element).closest(".input-validation-error").addClass("error");

        //$(element.select).closest("tr").addClass("error");
        //$(element).children().closest(".control-group").addClass("error");
        //$('.input-validation-error').closest(".control-group").addClass("error"); - Não funciona
    },
    unhighlight: function (element) {
        $(element).closest(".control-group").removeClass("error");
        $(element).closest(".editor-field").removeClass("error");
        $(element).closest(".form-control").removeClass("error");   
        $(element).closest(".input-validation-error").removeClass("error");

        //$("select").closest(".control-group").removeClass("error");
        //$('.input-validation-error').closest(".control-group").removeClass("error");
    }
});


/*
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
|                                  FILTER OF EVENT LIST                                |
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
*/

//APPLY FILTER 
$("#EventTypeIdEv").change(function () {
    $("#EventTypeIdEv option:selected").each(function () { LoadEventList(); });
});
//--------------------------------------------
$('#txtTitleEv').keydown(function (e) {
    var $this = $(this);
    setTimeout(function() {
        var text = $this.val();
        //alert(text.length);
        if (text.length >= 3) {
            LoadEventList();
        }
        if (text.length < 1) {
            LoadEventList();
        }
    }, 0);
});
//--------------------------------------------
$("#txtDateStartEv").focusout(function () { LoadEventList(); });
$("#txtDateEndEv").focusout(function () { LoadEventList(); });
//--------------------------------------------
//PeriodId - Format as first option disabled
$('#PeriodIdEv').addClass('DisableFirstOptionDDL');
$('#PeriodIdEv :eq(0)').val('0').addClass('DisableFirstOptionDDL');
$('#PeriodIdEv').on('click', function () {
    $('#PeriodIdEv').removeClass('DisableFirstOptionDDL');
});
//PeriodId - Event OnChange
$('#PeriodIdEv').on('change', function () {
    //alert($("#PeriodIdEv option:selected").val());
    var valPeriodId = $("#PeriodIdEv option:selected").val();
    if (valPeriodId == '0') {
        $('#PeriodIdEv').addClass('DisableFirstOptionDDL');
        $('#PeriodIdEv').removeClass('DisableFirstOptionDDL');
        //$("#txtDateStartEv").val("01/01/2014 0:00"); //PELO SISTEMA
        //$("#txtDateEndEv").val("31/12/2014 23:59"); //PELO SISTEMA
        $("#txtDateStartEv").prop("disabled", false);
        $("#txtDateEndEv").prop("disabled", false);
        //alert('é zero');
        LoadEventList();
    } else {
        //alert('não é zero');
        $('#PeriodIdEv').removeClass('DisableFirstOptionDDL');
        $("#txtDateStartEv").val(""); //PELO SISTEMA
        $("#txtDateEndEv").val(""); //PELO SISTEMA
        $("#txtDateStartEv").prop("disabled", true);
        $("#txtDateEndEv").prop("disabled", true);
        LoadEventList();
    }
});
//--------------------------------------------
$("#PlaceIdEv").change(function () {
    $("#PlaceIdEv option:selected").each(function () { LoadEventList(); });
});
//--------------------------------------------
$("#StateIdEv").change(function () {
    $("#PlaceIdEv option:selected").each(function () { LoadEventList(); });
});
//--------------------------------------------
$("input[name=radPublic]:radio").change(function () { LoadEventList(); });
//--------------------------------------------
$("#EntityIdEv").change(function () {
    $("#PlaceIdEv option:selected").each(function () { LoadEventList(); });
});
//--------------------------------------------

/* LOAD EVENT LIST */
function LoadEventList() {
    var ddlType = 0;
    $("#EventTypeIdEv option:selected").each(function () {
        ddlType = $(this).text();
    });

    var txtTitle = $("#txtTitleEv").val();

    var txtDate = "";
    var txtDateStart = $("#txtDateStartEv").val();
    var txtDateEnd = $("#txtDateEndEv").val();

    var ddlPeriod = "";
    $("#PeriodIdEv option:selected").each(function () {
        ddlPeriod = $(this).attr("value");
    });

    var ddlPlace = 0;
    $("#PlaceIdEv option:selected").each(function () {
        ddlPlace = $(this).text();
    });

    var ddlState = 0;
    $("#StateIdEv option:selected").each(function () {
        ddlState = $(this).text();
    });

    var radPublic = $("input[name='radPublic']:checked").val();

    var ddlEntity = 0;
    $("#EntityIdEv option:selected").each(function () {
        ddlEntity = $(this).text();
    });

    //alert("Type: " + ddlType + " Title: " + txtTitle);
    var stringFilter = ddlType + "," + txtTitle + "," + txtDate + "," + txtDateStart + "," + txtDateEnd + "," + ddlPeriod + "," + ddlPlace + "," + ddlState + "," + radPublic + "," + ddlEntity;
    //alert(stringFilter);

    $.ajax({
        url: '/Event/IndexPv',
        dataType: 'html',
        type: 'GET',
        data: { stringFilter: stringFilter },
        success: function (data) {
            //alert( "Okay: " + stringFilter);
            $('#tabEvents').html(data); //<div id="edit_x">

        },
        error: function () {
            alert("ERRO ao atualizar lista de Eventos...");
        }

    });
}


//Clear Filters of Event List
function ClearFiltersEventList() {

    $("select#EntityIdEv").prop('selectedIndex', 0);
    $("select#StateIdEv").prop('selectedIndex', 0);
    $("select#PlaceIdEv").prop('selectedIndex', 0);
    $("select#CityIdEv").prop('selectedIndex', 0);
    $("select#ParticipIdEv").prop('selectedIndex', 0);
    $("select#EventTypeIdEv").prop('selectedIndex', 0);
    $("select#EntityTypeIdEv").prop('selectedIndex', 0);
    $("select#EntityIdEv").prop('selectedIndex', 0);
    $("select#ParticipantTypeIdEv").prop('selectedIndex', 0);

    //$("input[name='radPublic']").val("na");
   // $("input[name='radPublic'][value='na']").attr('checked', 'checked');
    $('input:radio[name=radPublic]').filter('[value=na]').prop('checked', true);

    $("#txtTitleEv").val("");
    $("#txtDateStartEv").val(""); //PELO SISTEMA
    $("#txtDateEndEv").val(""); //PELO SISTEMA
    $("select#PeriodIdEv").prop('selectedIndex', 0);
    $('#PeriodIdEv').addClass('DisableFirstOptionDDL');
    
    LoadEventList();
}

/*
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
|                      DATATIMEPICKER - FILTER OF VIEW EVENT LIST                      |
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
*/

var dtsEv = $('#datetimepickerStartEv').datetimepicker({
    format: 'dd/MM/yyyy hh:mm',
    language: 'pt-PT',
    pickTime: true
}).on('changeDate', function (e) {
    //$(this).datetimepicker('hide');
    //$(this).setTimeout('0:00');
    LoadEventList();
}).data('datetimepicker');

var dteEv = $('#datetimepickerEndEv').datetimepicker({
    format: 'dd/MM/yyyy hh:mm',
    language: 'pt-PT',
    pickTime: true
    //startDate: dts.getLocalDate()
}).on('changeDate', function (e) {
    //$(this).datetimepicker('hide');
    //$(this).setTimeout('23:59');
    LoadEventList();
}).data('datetimepicker');

/*
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
|                              DATATIMEPICKER - EVENT FORM                             |
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
*/

var dts = $('#datetimepickerStart').datetimepicker({
    format: 'dd/MM/yyyy hh:mm',
    language: 'pt-PT',
    pickTime: true
}).on('changeDate', function (e) {
}).data('datetimepicker');

var dte = $('#datetimepickerEnd').datetimepicker({
    format: 'dd/MM/yyyy hh:mm',
    language: 'pt-PT',
    pickTime: true
}).on('changeDate', function (e) {
}).data('datetimepicker');

var dtses = $('#datetimepickerSes').datetimepicker({
    format: 'dd/MM/yyyy hh:mm',
    language: 'pt-PT',
    pickTime: true
}).on('changeDate', function (e) {
}).data('datetimepicker');

/*
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
|                              DATATIMEPICKER - ACCREQUEST                             |
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
*/

var dts = $('#datetimepickerReqDate').datetimepicker({
    format: 'dd/MM/yyyy hh:mm',
    language: 'pt-PT',
    pickTime: true
}).on('changeDate', function (e) {
}).data('datetimepicker');

var dte = $('#datetimepickerFeedback').datetimepicker({
    format: 'dd/MM/yyyy hh:mm',
    language: 'pt-PT',
    pickTime: true
}).on('changeDate', function (e) {
}).data('datetimepicker');

/*
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
|                            ADD AND DELETE POSTER OF EVENT                            |
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
*/
//$('#DelPoster').click(function () { DelPoster(); return false; });
//$('#AddPoster').click(function () { AddPoster(); return false; });

//function DelPoster() {
//    alert("treste... DEL");
//    document.getElementById("ImgPoster").src = "/Images/Icones/icon_no_image.png";
//    document.getElementById("Poster").value = "/Images/Icones/icon_no_image.png";
//}

//function AddPoster() {
//    alert("treste... ADD");
//    document.getElementById("ImgPoster").src = document.getElementById("Poster").value;
//}


/*
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
|                            SELECT LINE THE LIST OF EVENTS                            |
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
*/
$('#events tbody tr').on('click', function () {
    //$("table").addClass("table table-hover table-striped table-bordered");
    var idEvent = $(this).attr("id");
    registSelect = 1;
    
    $(this).closest('table').find('td').removeClass('SelectLineTable');
    $(this).find('td').addClass('SelectLineTable');

    var urlEditEvent = "/Event/Edit/" + idEvent;
    var urlDelEvent = "/Event/Delete/" + idEvent;
    document.getElementById("btnEdit").href = urlEditEvent;
    document.getElementById("btnDel").href = urlDelEvent;
    
});


/*
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
|                         DOUBLE CLICK THE LINE THE LIST OF EVENTS                     |
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
*/
$("#events tbody").on("dblclick", "tr", function () {
    var idEvent = $(this).attr("id");
    //$(location).href("/Event/Edit/" + idEvent);
    var url = "/Event/Edit/" + idEvent;
    $(location).attr('href', url);
});

/*
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
|                            SELECT LINE THE LIST OF USERS                             |
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
*/

$('#usersTab tbody tr').on('click', function () {
    
    var idUser = $(this).attr("id");
    registSelect = 1;
    
    $(this).closest('table').find('td').removeClass('SelectLineTable');
    $(this).find('td').addClass('SelectLineTable');

    var urlEditUser = "/Account/Edit/" + idUser;
    var urlDelUser = "/Account/Delete/" + idUser;
    document.getElementById("btnEditUser").href = urlEditUser;
    document.getElementById("btnDelUser").href = urlDelUser;
    
    //document.getElementById("btnEdit").switchClass('btn btn-primary disabled', 'btn btn-primary', 'Fast');
});


/*
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
|                         DOUBLE CLICK THE LINE THE LIST OF USERS                      |
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
*/
//$("#users tbody").on("dblclick", "tr", function () {
//    var idUser = $(this).attr("id");
//    var url = "/Account/Manage/" + idUser;
//    $(location).attr('href', url);
//});

$('#usersTab table.table-striped tbody tr').on('dblclick', function () {
    alert("double click User!");

    var idReg = $(this).attr("id");

    $.ajax({
        url: '/Account/Edit',
        dataType: 'html',
        type: 'GET',
        data: { page: 1, id: idReg },
        success: function (data) {
            $('#userManager').html(data);
            jQuery.validator.unobtrusive.parse('#userManager');
            UserBgColorEdit();
        },
        error: function () {
            alert("ERRO: ao atualizar a lista dos utilizadores.");
        }
    });

});



/*
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
|                                   UPDATE LIST USERS                                  |
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
*/
function UpdateListUsers() {
    $.ajax({
        url: '/Account/ListUsersPv',
        dataType: 'html',
        type: 'GET',
        success: function (data) {
            $('#listUsers').html(data);
            //jQuery.validator.unobtrusive.parse('#listUsers');

            UserBgColorClear();
        },
        error: function () {
            alert("ERRO: ao tentar carregar formulário...");
        }
    });
}

/*
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
|                     CHANGE BACKGROUND OF PANEL RIGHT OF USERS                        |
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
*/


//$("#btnAddUser").on("click", null, function (e) {
function UserBgColorAdd() {
    //alert("add user");
    $("#userManager").removeClass("alert");
    $("#userManager").removeClass("alert-danger");
    $("#userManager").addClass("alert-success");
    jQuery.validator.unobtrusive.parse('#userManager');
};

function UserBgColorEdit() {
    //alert("edit user");
    var url = $('#btnEditUser').attr('href');
    var rSplit = url.split('/');

    if (typeof rSplit[3] !== 'undefined') {
        $("#userManager").removeClass("alert-success");
        $("#userManager").removeClass("alert-danger");
        $("#userManager").addClass("alert");
        jQuery.validator.unobtrusive.parse('#userManager');
    }
};

function UserBgColorDel() {
    //alert("delete user");
    var url = $('#btnDelUser').attr('href');
    var rSplit = url.split('/');


    if (typeof rSplit[3] !== 'undefined') {
        $("#userManager").removeClass("alert-success");
        $("#userManager").removeClass("alert");
        $("#userManager").addClass("alert-danger");
        jQuery.validator.unobtrusive.parse('#userManager');
    }
};

function UserBgColorClear() {
    $("#userManager").removeClass("alert-success");
    $("#userManager").removeClass("alert");
    $("#userManager").removeClass("alert-danger");
}

/*
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
|                          SELECT LINE THE LIST OF PARAMETERS                          |
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
*/
$('#tabParam tbody tr').on('click', function () {
    //$("table").addClass("table table-hover table-striped table-bordered");
    var idReg = $(this).attr("id");
    registSelect = 1;
    
    $(this).closest('table').find('td').removeClass('SelectLineTable');
    $(this).find('td').addClass('SelectLineTable');

    var page = getUrlVars("param")["page"];
    var idParam = getUrlVars("param")["idParam"];

    document.getElementById("btnEditParam").href = "/Parameters/EditGeneric" + "?page=" + page + "&idParam=" + idParam + "&id=" + idReg;
    document.getElementById("btnDelParam").href = "/Parameters/DeleteGeneric" + "?page=" + page + "&idParam=" + idParam + "&id=" + idReg;
    //document.getElementById("btnEditParam").switchClass('btn btn-primary disabled', 'btn btn-primary', 'Fast');

});


/*
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
|                       DOUBLE CLICK IN LINE THE LIST OF PARAMETERS                    |
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
*/
$("#tabParam tbody").on("dblclick", "tr", function () {
    var idReg = $(this).attr("id");
    
    var ddlParam = 0;
    $("#Parameters option:selected").each(function () {
        ddlParam = $(this).val();
    });

    
    $.ajax({
        url: '/Parameters/EditGeneric',
        dataType: 'html',
        type: 'GET',
        data: { page: 1, idParam: ddlParam, id: idReg },
        success: function (data) {
            $('#formParam').html(data);
            jQuery.validator.unobtrusive.parse('#formParam');
            ParamBgColorEdit();
        },
        error: function () {
            alert("ERRO: ao atualizar a lista dos Parâmetros.");
        }
    });
});


/*
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
|               GET URL variables and return them as an associative array              |
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
*/
function getUrlVars(type)
{
    var vars = [], hash;
    //var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    var hashes;
    
    switch (type) {
        case "param":
            hashes = document.getElementById("btnEditParam").href.slice(document.getElementById("btnEditParam").href.indexOf('?') + 1).split('&');
            break;
        case "prop":
            hashes = document.getElementById("btnEditProp").href.slice(document.getElementById("btnEditProp").href.indexOf('?') + 1).split('&');
            break;
        case "ses":
            hashes = document.getElementById("btnEditSes").href.slice(document.getElementById("btnEditSes").href.indexOf('?') + 1).split('&');
            break;
        case "par":
            hashes = document.getElementById("btnEditPar").href.slice(document.getElementById("btnEditPar").href.indexOf('?') + 1).split('&');
            break;
        case "res":
            hashes = document.getElementById("btnEditRes").href.slice(document.getElementById("btnEditRes").href.indexOf('?') + 1).split('&');
            break;
        case "com":
            hashes = document.getElementById("btnEditCom").href.slice(document.getElementById("btnEditCom").href.indexOf('?') + 1).split('&');
            break;
        case "user":
            hashes = document.getElementById("btnEditUser").href.slice(document.getElementById("btnEditUser").href.indexOf('?') + 1).split('&');
            break;
        default:
            hashes = document.getElementById("btnEdit").href.slice(document.getElementById("btnEdit").href.indexOf('?') + 1).split('&');
            break;
    }

    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

/*
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
|                             UPDATE LIST AREA OF PARAMETERS                           |
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
*/
function UpdateListParam(pag, idP) {
    //alert("P: " + pag);
        
    $.ajax({
        url: '/Parameters/ListParameters',
        dataType: 'html',
        type: 'GET',
        data: {idParam: idP, page: pag},
        success: function (data) {           
            $('#listParam').html(data);
            ParamBgColorClear();
        },
        error: function () {
            alert("ERRO: ao tentar carregar formulário...");
            ParamBgColorClear();
        }
    });   
}

function UpdateListParamModal(pag, idP) {
    //alert("P: " + pag);

    $.ajax({
        url: '/Parameters/ListParameters',
        dataType: 'html',
        type: 'GET',
        data: { idParam: idP, page: pag },
        success: function (data) {
            $('#paramModalLeft').html(data);
            jQuery.validator.unobtrusive.parse('#listParam');
            ParamBgColorClear();
        },
        error: function () {
            alert("ERRO: ao tentar carregar formulário...");
            ParamBgColorClear();
        }
    });
}
/*
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
|                            CHANGE ACTION OF DDL PARAMENTER                           |
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
*/
//document.getElementById("Parameters").onchange = function () {
$('#Parameters').on('change', function () {
    //Update Left Panel
    //var e = document.getElementById("DdlParameter");
    //var ddlParam = e.options[e.selectedIndex].value;

    var ddlParam = 0;
    $("#Parameters option:selected").each(function () {
        ddlParam = $(this).val();
    });

    //alert("ID PARAM.: " + ddlParam);

    $.ajax({
        url: '/Parameters/ListParameters',
        dataType: 'html',
        type: 'GET',
        data: { idParam: ddlParam, page: "1" },

        success: function(data) {
            $('#listParam').html(data);
            jQuery.validator.unobtrusive.parse('#listParam');
        },
        error: function() {
            alert("ERRO: ao tentar abrir formulário...");
            ParamBgColorClear();
        }
    });

    //Update Right Panel
    $.ajax({
        url: '/Parameters/DefaultRight',
        dataType: 'html',
        type: 'GET',
        data: { idParam: ddlParam},
        success: function(data) {
            $('#formParam').html(data);
            jQuery.validator.unobtrusive.parse('#formParam');
            ParamBgColorClear();
        },
        error: function() {
            alert("ERRO: ao tentar abrir formulário...");
            ParamBgColorClear();
        }
    });
    
});


/*
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
|                  CHANGE BACKGROUND OF PANEL RIGHT OF PARAMETERS                      |
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
*/

function ParamBgColorAdd() {
    //alert("add...");
    $("#panelRight").removeClass("alert");
    $("#panelRight").removeClass("alert-danger");
    $("#panelRight").addClass("alert-success");
    jQuery.validator.unobtrusive.parse('#panelRight');
}

function ParamBgColorEdit() {
    //alert("edit...");
    var url = $('#btnEditParam').attr('href');
    var rSplit = url.split('&');

    if (typeof rSplit[2] !== 'undefined') {
        $("#panelRight").removeClass("alert-success");
        $("#panelRight").removeClass("alert-danger");
        $("#panelRight").addClass("alert");
        jQuery.validator.unobtrusive.parse('#panelRight');
    }
}

function ParamBgColorDel() {
    //alert("del...");
    var url = $('#btnEditParam').attr('href');
    var rSplit = url.split('&');

    if (typeof rSplit[1] !== 'undefined') {
        $("#panelRight").removeClass("alert-success");
        $("#panelRight").removeClass("alert");
        $("#panelRight").addClass("alert-danger");
        jQuery.validator.unobtrusive.parse('#panelRight');
    }
}

function ParamBgColorClear() {
    $("#panelRight").removeClass("alert-success");
    $("#panelRight").removeClass("alert");
    $("#panelRight").removeClass("alert-danger");
}

//MODAL VIEW //////////////////////////////////////////////

function ParamBgColorAddModal() {
    //alert("add...");
    $("#panelRight").removeClass("alert");
    $("#panelRight").removeClass("alert-danger");
    $("#panelRight").addClass("alert-success");
    jQuery.validator.unobtrusive.parse('#tabEventResRight');
}
function ParamBgColorEditModal() {
    //alert("edit...");
    var url = $('#btnEditParam').attr('href');
    var rSplit = url.split('&');

    if (typeof rSplit[2] !== 'undefined') {
        $("#panelRight").removeClass("alert-success");
        $("#panelRight").removeClass("alert-danger");
        $("#panelRight").addClass("alert");
        jQuery.validator.unobtrusive.parse('#panelRight');
    }
}
function ParamBgColorDelModal() {
    //alert("del...");
    var url = $('#btnEditParam').attr('href');
    var rSplit = url.split('&');

    if (typeof rSplit[1] !== 'undefined') {
        $("#panelRight").removeClass("alert-success");
        $("#panelRight").removeClass("alert");
        $("#panelRight").addClass("alert-danger");
        jQuery.validator.unobtrusive.parse('#panelRight');
    }
}
function ParamBgColorClearModal() {
    $("#panelRight").removeClass("alert-success");
    $("#panelRight").removeClass("alert");
    $("#panelRight").removeClass("alert-danger");
}
// //////////////////////////////////////////////////////

function CheckSelectParam() {
    
    if (registSelect == 1) {
        $("#panelRight").removeClass("alert-success");
        $("#panelRight").removeClass("alert-danger");
        $("#panelRight").addClass("alert");
        
        $("#msgWar").html("Selecione um registo");
        $("#msgWar").removeClass("alert");
        $("#msgWar").addClass("alert-info");
        
        return true;
        
    } else {
        $("#msgInf").html("Selecione um registo!");
        $("#msgInf").removeClass("alert-info");
        $("#msgInf").addClass("alert");
        
        $("#msgSuc").html("Selecione um registo!");
        $("#msgSuc").removeClass("alert-success");
        $("#msgSuc").addClass("alert");
        
        $("#msgErr").html("Selecione um registo!");
        $("#msgErr").removeClass("alert-error");
        $("#msgErr").addClass("alert");

        return false;
    }
}


/*
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
|                           FUNCTIONS OF STATISTIC MODULE                              |
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
*/

//View List Mode
function ViewListMode() {
    $("#ChartMode").hide();
    $("#ListMode").show();      
}

//View Chart Mode
function ViewChartMode() {
    $("#ChartMode").show();
    $("#ListMode").hide();
        
    //Adjust the Graph to the dimensions of the previous DIV
    $("#ChartMode").children('div').css('width', $("#ChartMode").width());
    $("#ChartMode").children('div').css('height', $("#panelData").height());
    drawChart();
}
    

//DropDownList GroupFiled Action: Change Value
$("#GroupField").change(function () {
    LoadStatResult();
    ClearFilters();
        
    var ddlGrupField = 0;
    $("#GroupField option:selected").each(function () {
        ddlGrupField = $(this).val();
    });

    switch (ddlGrupField) {
        case "0":
            //$("#txtDateStartSt").prop("disabled", true);
            //$("#txtDateEndSt").prop("disabled", true);
            $("#EntityIdSt").prop("disabled", true);
            $("#StateIdSt").prop("disabled", true);
            $("#PlaceIdSt").prop("disabled", true);
            $("#CityIdSt").prop("disabled", true);
            $("#ParticipIdSt").prop("disabled", true);
            $("#EventTypeIdSt").prop("disabled", true);
            $("#EntityTypeIdSt").prop("disabled", true);
            $("#ParticipantTypeIdSt").prop("disabled", true);
            break;
        case "1":
            //$("#txtDateStartSt").prop("disabled", false);
            //$("#txtDateEndSt").prop("disabled", false);
            $("#EntityIdSt").prop("disabled", true);
            $("#StateIdSt").prop("disabled", false);
            $("#PlaceIdSt").prop("disabled", false);
            $("#CityIdSt").prop("disabled", false);
            $("#ParticipIdSt").prop("disabled", false);
            $("#EventTypeIdSt").prop("disabled", false);
            $("#EntityTypeIdSt").prop("disabled", false);
            $("#ParticipantTypeIdSt").prop("disabled", false);
            break;
        case "2":
            //$("#txtDateStartSt").prop("disabled", false);
            //$("#txtDateEndSt").prop("disabled", false);
            $("#EntityIdSt").prop("disabled", false);
            $("#StateIdSt").prop("disabled", true);
            $("#PlaceIdSt").prop("disabled", false);
            $("#CityIdSt").prop("disabled", false);
            $("#ParticipIdSt").prop("disabled", false);
            $("#EventTypeIdSt").prop("disabled", false);
            $("#EntityTypeIdSt").prop("disabled", false);
            $("#ParticipantTypeIdSt").prop("disabled", false);
            break;
        case "3":
            //$("#txtDateStartSt").prop("disabled", false);
            //$("#txtDateEndSt").prop("disabled", false);
            $("#EntityIdSt").prop("disabled", false);
            $("#StateIdSt").prop("disabled", false);
            $("#PlaceIdSt").prop("disabled", true);
            $("#CityIdSt").prop("disabled", false);
            $("#ParticipIdSt").prop("disabled", false);
            $("#EventTypeIdSt").prop("disabled", false);
            $("#EntityTypeIdSt").prop("disabled", false);
            $("#ParticipantTypeIdSt").prop("disabled", false);
            break;
        case "5":
            //$("#txtDateStartSt").prop("disabled", false);
            //$("#txtDateEndSt").prop("disabled", false);
            $("#EntityIdSt").prop("disabled", false);
            $("#StateIdSt").prop("disabled", false);
            $("#PlaceIdSt").prop("disabled", true);
            $("#CityIdSt").prop("disabled", true);
            $("#ParticipIdSt").prop("disabled", false);
            $("#EventTypeIdSt").prop("disabled", false);
            $("#EntityTypeIdSt").prop("disabled", false);
            $("#ParticipantTypeIdSt").prop("disabled", false);
            break;
        case "6":
            //$("#txtDateStartSt").prop("disabled", false);
            //$("#txtDateEndSt").prop("disabled", false);
            $("#EntityIdSt").prop("disabled", false);
            $("#StateIdSt").prop("disabled", false);
            $("#PlaceIdSt").prop("disabled", false);
            $("#CityIdSt").prop("disabled", false);
            $("#ParticipIdSt").prop("disabled", true);
            $("#EventTypeIdSt").prop("disabled", false);
            $("#EntityTypeIdSt").prop("disabled", false);
            $("#ParticipantTypeIdSt").prop("disabled", false);
            break;
        case "8":
            //$("#txtDateStartSt").prop("disabled", false);
            //$("#txtDateEndSt").prop("disabled", false);
            $("#EntityIdSt").prop("disabled", false);
            $("#StateIdSt").prop("disabled", false);
            $("#PlaceIdSt").prop("disabled", false);
            $("#CityIdSt").prop("disabled", false);
            $("#ParticipIdSt").prop("disabled", false);
            $("#EventTypeIdSt").prop("disabled", true);
            $("#EntityTypeIdSt").prop("disabled", false);
            $("#ParticipantTypeIdSt").prop("disabled", false);
            break;
        case "9":
            //$("#txtDateStartSt").prop("disabled", false);
            //$("#txtDateEndSt").prop("disabled", false);
            $("#EntityIdSt").prop("disabled", false);
            $("#StateIdSt").prop("disabled", false);
            $("#PlaceIdSt").prop("disabled", false);
            $("#CityIdSt").prop("disabled", false);
            $("#ParticipIdSt").prop("disabled", false);
            $("#EventTypeIdSt").prop("disabled", false);
            $("#EntityTypeIdSt").prop("disabled", true);
            $("#ParticipantTypeIdSt").prop("disabled", false);
            break;
        case "10":
            //$("#txtDateStartSt").prop("disabled", false);
            //$("#txtDateEndSt").prop("disabled", false);
            $("#EntityIdSt").prop("disabled", true);
            $("#StateIdSt").prop("disabled", false);
            $("#PlaceIdSt").prop("disabled", false);
            $("#CityIdSt").prop("disabled", false);
            $("#ParticipIdSt").prop("disabled", false);
            $("#EventTypeIdSt").prop("disabled", false);
            $("#EntityTypeIdSt").prop("disabled", false);
            $("#ParticipantTypeIdSt").prop("disabled", true);
            break;
        default:
            //$("#txtDateStartSt").prop("disabled", false);
            //$("#txtDateEndSt").prop("disabled", false);
            $("#EntityIdSt").prop("disabled", false);
            $("#StateIdSt").prop("disabled", false);
            $("#PlaceIdSt").prop("disabled", false);
            $("#CityIdSt").prop("disabled", false);
            $("#ParticipIdSt").prop("disabled", false);
            $("#EventTypeIdSt").prop("disabled", false);
            $("#EntityTypeIdSt").prop("disabled", false);
            $("#ParticipantTypeIdSt").prop("disabled", false);
            break;
    }
});
    
//Load of Statistics Result
function LoadStatResult() {
  
    var ddlGrupField = 0;
    $("#GroupField option:selected").each(function () {
        ddlGrupField = $(this).val();
    });
        
    var ddlEntity = 0;
    $("#EntityIdSt option:selected").each(function () {
        ddlEntity = $(this).text();
    });
        
    var ddlState = 0;
    $("#StateIdSt option:selected").each(function () {
        ddlState = $(this).text();
    });
        
    var ddlPlace = 0;
    $("#PlaceIdSt option:selected").each(function () {
        ddlPlace = $(this).text();
    });
        
    var ddlCity = 0;
    $("#CityIdSt option:selected").each(function () {
        ddlCity = $(this).text();
    });
        
    var ddlParticip = 0;
    $("#ParticipIdSt option:selected").each(function () {
        ddlParticip = $(this).text();
    });
        
    var ddlType = 0;
    $("#EventTypeIdSt option:selected").each(function () {
        ddlType = $(this).text();
    });
        
    var ddlEntityType = 0;
    $("#EntityTypeIdSt option:selected").each(function () {
        ddlEntityType = $(this).text();
    });
        
    var ddlParticipantType = 0;
    $("#ParticipantTypeIdSt option:selected").each(function () {
        ddlParticipantType = $(this).text();
    });

    var txtDateStart = $("#txtDateStartSt").val();
        
    var txtDateEnd = $("#txtDateEndSt").val();
    
    //alert("Type: " + ddlType + " Title: " + txtTitle);
    var stringFilter = ddlGrupField + "," + ddlEntity + "," + ddlState + "," + ddlPlace + "," + ddlCity + "," + ddlParticip + "," + ddlType + "," + ddlEntityType + "," + ddlParticipantType + "," + txtDateStart + "," + txtDateEnd;
    //alert(stringFilter);
            
    $.ajax({
        url: '/Statistics/StaticDate',
        dataType: 'html',
        type: 'GET',
        data: { stringFilter: stringFilter },
        success: function (data) {
            //alert( "Okay: " + stringFilter);
            $('#resultData').html(data); //<div id="edit_x">
            //alert('DDL GROUP FIELD: ' + ddlGrupField);
            //Disable the buttons modes of visualization
            if (ddlGrupField == 0 || ddlGrupField == null) {
                //alert("DISABLED....");
                $("#btnListMode").addClass("disabled");
                $("#btnChartMode").addClass("disabled");
                $('#btnClearFilters').addClass('disabled');
                    
                $("#txtDateStartSt").prop("disabled", true);
                $("#txtDateEndSt").prop("disabled", true);
                $("#EntityIdSt").prop("disabled", true);
                $("#StateIdSt").prop("disabled", true);
                $("#PlaceIdSt").prop("disabled", true);
                $("#CityIdSt").prop("disabled", true);
                $("#ParticipIdSt").prop("disabled", true);
                $("#EventTypeIdSt").prop("disabled", true);
                $("#EntityTypeIdSt").prop("disabled", true);
                $("#ParticipantTypeIdSt").prop("disabled", true);
            } else {
                $("#btnListMode").removeClass("disabled");
                $("#btnChartMode").removeClass("disabled");
                $('#btnClearFilters').removeClass('disabled');
             
            }
            
            //alert("FL: @ViewBag.Title");
            //$("#FilterList")

        },
        error: function () {
            alert("Ocorreu um erro ao calcular os dados estatísticos!");
                
            //Disable the buttons modes of visualization
            $("#btnListMode").addClass("disabled");
            $("#btnChartMode").addClass("disabled");
        }

    });
}
    
//Clear Fields of Filter
function ClearFilters() {
    //alert("limpou");
    $('#FilterList').find('option').remove().end().append($('<option value="0">Sem filtros selecionados...</option>'));
    
    $("select#EntityIdSt").prop('selectedIndex', 0);
    $("select#StateIdSt").prop('selectedIndex', 0);
    $("select#PlaceIdSt").prop('selectedIndex', 0);
    $("select#CityIdSt").prop('selectedIndex', 0);
    $("select#ParticipIdSt").prop('selectedIndex', 0);
    $("select#EventTypeIdSt").prop('selectedIndex', 0);
    $("select#EntityTypeIdSt").prop('selectedIndex', 0);
    $("select#EntityIdSt").prop('selectedIndex', 0);
    $("select#ParticipantTypeIdSt").prop('selectedIndex', 0);

    //var d = new Date();
    //alert('01/01/' + d.getFullYear());
    
    $("#txtDateStartSt").val("01/01/2014"); //PELO SISTEMA
    $("#txtDateEndSt").val("31/12/2014"); //PELO SISTEMA
    
    //$('#btnClearFilters').addClass('disabled');
    
    LoadStatResult();
}

//Action OnClick of Clear Filters Button
$("#btnClearFilters").click(function () {
    if (!$("#btnClearFilters").hasClass('disabled')) {
        ClearFilters();
    }
});


//Applying Filters
function ApplyFilter(f, it) {
    var valueStr = it.text();
    
    if ($('#FilterList :eq(0)').val() == 0) { $('#FilterList :eq(0)').text("Remover Filtro:"); }

    var exist = false;
    var item = "";
    $("#FilterList option").each(function () {
        if ($(this).val() == f) {
            exist = true;
            item = $(this);
        }
    });
    
    if (exist) {
        item.text(valueStr);
    } else {
        $("#FilterList").append("<option value='" + f + "' class='FilterListEnable'>" + valueStr + "</option>");
    }
}

function ApplyFilterAux() {
    //$('#btnClearFilters').removeClass('disabled');
    LoadStatResult();
}

//Apply Filter: EntityId - 1
$("#EntityIdSt").change(function () {
    $("#EntityIdSt option:selected").each(function () { ApplyFilter(1, $(this)); });
    ApplyFilterAux();
});
//Apply Filter: StateId - 2
$("#StateIdSt").change(function () {
    $("#StateIdSt option:selected").each(function () { ApplyFilter(2, $(this)); });
    ApplyFilterAux();
});
//Apply Filter: PlaceId - 3
$("#PlaceIdSt").change(function () {
    $("#PlaceIdSt option:selected").each(function () { ApplyFilter(3, $(this)); });
    ApplyFilterAux();
});
//Apply Filter: CityId - 4
$("#CityIdSt").change(function () {
    $("#CityIdSt option:selected").each(function () { ApplyFilter(4, $(this)); });
    ApplyFilterAux();
});
//Apply Filter: ParticipId - 5
$("#ParticipIdSt").change(function () {
    $("#ParticipIdSt option:selected").each(function () { ApplyFilter(5, $(this)); });
    ApplyFilterAux();
});
//Apply Filter: EventTypeId - 6
$("#EventTypeIdSt").change(function () {
    $("#EventTypeIdSt option:selected").each(function () { ApplyFilter(6, $(this)); });
    ApplyFilterAux();
});
//Apply Filter: EntityTypeId - 7
$("#EntityTypeIdSt").change(function () {
    $("#EntityTypeIdSt option:selected").each(function () { ApplyFilter(7, $(this)); });
    ApplyFilterAux();
});
//Apply Filter: ParticipantTypeId - 8
$("#ParticipantTypeIdSt").change(function () {
    $("#ParticipantTypeIdSt option:selected").each(function () { ApplyFilter(8, $(this)); });
    ApplyFilterAux();
});
    

var dtsDate = new Date($('#txtDateStartSt').val());
var dteDate = new Date($('#txtDateEndSt').val());

//Fill in the field DateStart
var dts = $('#stDatetimepickerStart').datetimepicker({
    format: 'dd/MM/yyyy',
    language: 'pt-PT',
    pickTime: false
}).on('changeDate', function (ev) {
    var dtsDate = new Date($('#txtDateStartSt').val());
    var dteDate = new Date($('#txtDateEndSt').val());
    dts.hide();
    //alert("DI: " + dtsDate + " DF: " + dteDate);
        
    if (dtsDate <= dteDate) {
        //alert("OK...");
        LoadStatResult();
    } else {
        alert("ATENÇÃO:\n\n A «Data de Início» tem de ser inferior à «Data de Fim»!\n\n\n");
        //var newDate = new Date($('#txtDateEnd').val());
        //newDate.setDate(newDate.getDate() - 1);
        //dts.setValue(newDate);
    }
}).data('datetimepicker');


//Fill in the field DateEnd
var dte = $('#stDatetimepickerEnd').datetimepicker({
    format: 'dd/MM/yyyy',
    language: 'pt-PT',
    pickTime: false
}).on('changeDate', function (ev) {
    var dtsDate = new Date($('#txtDateStartSt').val());
    var dteDate = new Date($('#txtDateEndSt').val());
    dte.hide();
    //alert("DI: " + dtsDate + " DF: " + dteDate);
        
    if (dtsDate <= dteDate) {
        //alert("OKKKK...");
        LoadStatResult();
    } else {
        alert("ATENÇÃO:\n\n A «Data de Início» tem de ser inferior à «Data de Fim»!\n\n\n");
        //var newDate = new Date($('#txtDateStart').val());
        //newDate.setDate(newDate.getDate() + 1);
        //dte.setValue(newDate);
    }
}).data('datetimepicker');
   

/*
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
|                          Multiselect: Statistics Filters                             |
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
*/

$("#FilterList").change(function () {
	//alert("Numero de Opcoes: " + $("#FilterList option").length + " -- " + $("#FilterList :eq(0)").val());

    //Só tem duas 'options' e ainda não foi adicionada uma 'option' vazia com o 'value' == 0
    if ($("#FilterList option").length == 2 && $("#FilterList :eq(0)").val() != "0") {
	    $('#FilterList :eq(0)').text("Remover Filtro:");
	    $('#FilterList :eq(0)').attr('selected', true);
	    $('#btnClearFilters').removeClass('disabled');
	} else {//Só tem duas 'options' e a primeira é a vazia que adicionamos com o 'value' == 0
		if ($("#FilterList option").length == 2 && $("#FilterList :eq(0)").val() == "0") {
		    $("#FilterList :eq(0)").text("Sem filtros selecionados...");
		    ClearSpecificFilter();
		    $('#FilterList option:selected').remove();
		    $('#FilterList :eq(0)').attr('selected', true);
		    $('#btnClearFilters').addClass('disabled');
		} else {
		    $('#FilterList :eq(0)').text("Remover Filtro:");
		    ClearSpecificFilter();
		    $('#FilterList option:selected').remove();
		    $('#FilterList :eq(0)').attr('selected', true);
		    $('#btnClearFilters').removeClass('disabled');
		}
	}
});


function ClearSpecificFilter() {
        var id = 0;
        id = $('#FilterList option:selected').val();
        //alert("ID: " + id);

        switch (id) {
        case "1":
            $("select#EntityIdSt").prop('selectedIndex', 0);
            LoadStatResult();
            break;
        case "2":
            $("select#StateIdSt").prop('selectedIndex', 0);
            LoadStatResult();
            break;
        case "3":
            $("select#PlaceIdSt").prop('selectedIndex', 0);
            LoadStatResult();
            break;
        case "4":
            $("select#CityIdSt").prop('selectedIndex', 0);
            LoadStatResult();
            break;
        case "5":
            $("select#ParticipIdSt").prop('selectedIndex', 0);
            LoadStatResult();
            break;
        case "6":
            $("select#EventTypeIdSt").prop('selectedIndex', 0);
            LoadStatResult();
            break;
        case "7":
            $("select#EntityTypeIdSt").prop('selectedIndex', 0);
            LoadStatResult();
            break;
        case "8":
            $("select#ParticipantTypeIdSt").prop('selectedIndex', 0);
            LoadStatResult();
            break;
        }
}

/*
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
|                                 SUBMIT EVENT FORM                                    |
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
*/

//New Event (Create)
$("#btnSaveEvent").click(function () {

    //alert("gravar evento");
    
    var form = $('#formCreateEvent').serialize();

    $.ajax({
        url: '/Event/Create',
        dataType: 'html',
        type: 'POST',
        data: form,
        success: function(data) {
            //var href = $(location).attr('href');
            //var urlSp = href.split('/');
            //var url = urlSp[0] + "//" + urlSp[1] + urlSp[2] + "/" + urlSp[3];
            //window.location.href = url;

            $('body').html(data);

        },
        error: function () {
            alert("ERRO: ao tentar guardar o registo do evento.");
        }
    });
});

//Exist Event (Edit)
$("#btnSaveExistEvent").click(function () {
    //alert("gravar evento");

    var form = $('#formCreateEvent').serialize();

    $.ajax({
        url: '/Event/Edit',
        dataType: 'html',
        type: 'POST',
        data: form,
        success: function (data) {
            //var href = $(location).attr('href');
            //var urlSp = href.split('/');
            //var url = urlSp[0] + "//" + urlSp[1] + urlSp[2] + "/" + urlSp[3];
            //window.location.href = url;

            $('body').html(data);

        },
        error: function () {
            alert("ERRO: ao tentar guardar o registo do evento.");
        }
    });
});

/*
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
|                           MANAGEMENT OF EVENT PROPERTIES                             |
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
*/

//Update list of Event Properties
function UpdateEventProp(idEv) {
    //alert("IDev: " + idEv);

    $.ajax({
        url: '/EventPropertyValues/IndexPv',
        dataType: 'html',
        type: 'GET',
        data: {page: 1, idEvent: idEv },
        success: function (data) {  
            $('#tabEventPropLeft').html(data);
            //Atualizar backgroud do painel do lado direito
            PropBgColorClear();
        },
        error: function () {
            alert("ERRO: ao atualizar a lista das propriedades do evento.");
            PropBgColorClear();
        }
    });

}

//Select line the of Event Properties
$('#tableProp tbody tr').on('click', function () {
    var idReg = $(this).attr("id");
    registSelect = 1;
   
    $(this).closest('table').find('td').removeClass('SelectLineTable');
    $(this).find('td').addClass('SelectLineTable');
   
    var page = getUrlVars("prop")["page"];

    document.getElementById("btnEditProp").href = "/EventPropertyValues/EditPv" + "?page=" + page + "&id=" + idReg;
    document.getElementById("btnDelProp").href = "/EventPropertyValues/DeletePv" + "?page=" + page + "&id=" + idReg;
});


//DoubleClick Action in line the of Event Properties
$('#tableProp table.table-striped tbody tr').on('dblclick', function () {
    //alert("double click Prop.!");
    
    var idReg = $(this).attr("id");
    
    $.ajax({
        url: '/EventPropertyValues/EditPv',
        dataType: 'html',
        type: 'GET',
        data: {page: 1, id: idReg},
        success: function (data) {
            $('#tabEventPropRight').html(data);
            jQuery.validator.unobtrusive.parse('#tabEventPropRight');
            PropBgColorEdit();
        },
        error: function () {
            alert("ERRO: ao atualizar a lista das propriedades do evento.");
        }
    });
    
});



//Show field of Propertie Value
$("#EventPropertyId").change(function () {

    var id = 0;
    $("#EventPropertyId option:selected").each(function() {
        id = $(this).val();
    });

    //var idEvent = $("#EventId").val();
    
    //if (selectedEventProp2x) {
    //    AddProperty(idEvent);
    //}
    
    switch (id) {
        case "1":
            //alert("111");
            $("#ddl1").show();
            $("#ddl2").hide();
            $("#ddl3").hide();
            $("#ddl4").hide();
            $("#ddl5").hide();
            $("#txt").hide();
            $("#txta").hide();
            removeFields();
            $("#EventPropertyId").val(id); 
            break;
        case "2":
            //alert("222");
            $("#ddl1").hide();
            $("#ddl2").show();
            $("#ddl3").hide();
            $("#ddl4").hide();
            $("#ddl5").hide();
            $("#txt").hide();
            $("#txta").hide();
            removeFields();
            $("#EventPropertyId").val(id); 
            break;
        case "3":
            //alert("333");
            $("#ddl1").hide();
            $("#ddl2").hide();
            $("#ddl3").show();
            $("#ddl4").hide();
            $("#ddl5").hide();
            $("#txt").hide();
            $("#txta").hide();
            removeFields();
            $("#EventPropertyId").val(id);
            break;
        case "7":
            //alert("777");
            $("#ddl1").hide();
            $("#ddl2").hide();
            $("#ddl3").hide();
            $("#ddl4").show();
            $("#ddl5").hide();
            $("#txt").hide();
            $("#txta").hide();
            removeFields();
            $("#EventPropertyId").val(id);
            break;
        case "4": case "6":
            //alert("444 e 666");
            $("#ddl1").hide();
            $("#ddl2").hide();
            $("#ddl3").hide();
            $("#ddl4").hide();
            $("#ddl5").hide();
            $("#txt").show();
            $("#txta").hide();
            removeFields();
            $("#EventPropertyId").val(id);
            break;
        case "5": case "8": case "9":
            //alert("555 e 888 e 999");
            $("#ddl1").hide();
            $("#ddl2").hide();
            $("#ddl3").hide();
            $("#ddl4").hide();
            $("#ddl5").hide();
            $("#txt").hide();
            $("#txta").show();
            removeFields();
            $("#EventPropertyId").val(id);
            break;
        case "12": case "11": case "10":
            //alert("101010 e 11111 e 121212");
            $("#ddl1").hide();
            $("#ddl2").hide();
            $("#ddl3").hide();
            $("#ddl4").hide();
            $("#ddl5").show();
            $("#txt").hide();
            $("#txta").hide();
            removeFields();
            $("#EventPropertyId").val(id);
            break;
        default:
            //alert("default...");
            $("#ddl1").hide();;
            $("#ddl2").hide();
            $("#ddl3").hide();
            $("#ddl4").hide();
            $("#ddl5").hide();
            $("#txt").show();
            $("#txta").hide();
            removeFields();
            $("#EventPropertyId").val(id);
            break;
    }

    });

//Remove filds of view CreatePv / EventPropertyValues
function removeFields() {
    $('#divValue').children().each(function () {
        //alert($(this).attr("id"));
        if ($(this).css('display') == 'none') {
            $(this).remove();
        }
        selectedEventProp2x = true;
    });
}

// CHANGE BACKGROUND OF PANEL RIGHT OF PROVERTY VALUES            
function PropBgColorAdd() {
    //alert("add...");
    $("#tabEventPropRight").removeClass("alert");
    $("#tabEventPropRight").removeClass("alert-danger");
    $("#tabEventPropRight").addClass("alert-success");
    jQuery.validator.unobtrusive.parse('#tabEventPropRight');  
}

function PropBgColorEdit() {
    //alert("edit...");
 
    var url = $('#btnEditProp').attr('href');
    var rSplit = url.split('&');
    
    if (typeof rSplit[1] !== 'undefined') {
        $("#tabEventPropRight").removeClass("alert-success");
        $("#tabEventPropRight").removeClass("alert-danger");
        $("#tabEventPropRight").addClass("alert");
        jQuery.validator.unobtrusive.parse('#tabEventPropRight');
    }
}

function PropBgColorDel() {
    //alert("del...");
    var url = $('#btnEditProp').attr('href');
    var rSplit = url.split('&');

    if (typeof rSplit[1] !== 'undefined') {
        $("#tabEventPropRight").removeClass("alert-success");
        $("#tabEventPropRight").removeClass("alert");
        $("#tabEventPropRight").addClass("alert-danger");
        jQuery.validator.unobtrusive.parse('#tabEventPropRight');
    }  
}

function PropBgColorClear() {
    $("#tabEventPropRight").removeClass("alert-success");
    $("#tabEventPropRight").removeClass("alert");
    $("#tabEventPropRight").removeClass("alert-danger");
}

/*
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
|                            MANAGEMENT OF EVENT SESSIONS                              |
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
*/

//Update list of Event Sessions
function UpdateEventSes(idEv) {
    //alert("IDev: " + idEv);

    $.ajax({
        url: '/Session/IndexPv',
        dataType: 'html',
        type: 'GET',
        data: { page: 1, idEvent: idEv },
        success: function (data) {       
            $('#tabEventSesLeft').html(data);
            jQuery.validator.unobtrusive.parse('#tabEventSesLeft');
            //Atualizar cor de fundo do painel do lado direito
            SesBgColorClear();
        },
        error: function () {
            alert("ERRO: ao atualizar a lista das sessões do evento.");
            SesBgColorClear();
        }
    });

}

//Select line the of Event Sessions
$('#tableSess tbody tr').on('click', function () {
    var idReg = $(this).attr("id");
    registSelect = 1;

    $(this).closest('table').find('td').removeClass('SelectLineTable');
    $(this).find('td').addClass('SelectLineTable');

    var page = getUrlVars("ses")["page"];

    //alert("select session line : " + idReg);
    
    document.getElementById("btnEditSes").href = "/Session/EditPv" + "?page=" + page + "&id=" + idReg;
    document.getElementById("btnDelSes").href = "/Session/DeletePv" + "?page=" + page + "&id=" + idReg;
});

//DoubleClick Action in line the of Event Session
$('#tableSess table.table-striped tbody tr').on('dblclick', function () {
    //alert("double click!");

    var idReg = $(this).attr("id");

    $.ajax({
        url: '/Session/EditPv',
        dataType: 'html',
        type: 'GET',
        data: { page: 1, id: idReg },
        success: function (data) {
            $('#tabEventSesRight').html(data);
            jQuery.validator.unobtrusive.parse('#tabEventSesRight');
            //Atualizar backgroud do painel do lado direito
            SesBgColorEdit();
        },
        error: function () {
            alert("ERRO: ao atualizar a lista das sessões do evento.");
        }
    });

});

// CHANGE BACKGROUND OF PANEL RIGHT OF SESSIONS            
function SesBgColorAdd() {
    //alert("add...");

    $("#tabEventSesRight").removeClass("alert");
    $("#tabEventSesRight").removeClass("alert-danger");
    $("#tabEventSesRight").addClass("alert-success");
    jQuery.validator.unobtrusive.parse('#tabEventSesRight');
}

function SesBgColorEdit() {
    //alert("edit...");
    var url = $('#btnEditSes').attr('href');
    var rSplit = url.split('&');

    if (typeof rSplit[1] !== 'undefined') {
        $("#tabEventSesRight").removeClass("alert-success");
        $("#tabEventSesRight").removeClass("alert-danger");
        $("#tabEventSesRight").addClass("alert");
        jQuery.validator.unobtrusive.parse('#tabEventSesRight');
    }
}

function SesBgColorDel() {
    //alert("del...");
    var url = $('#btnEditSes').attr('href');
    var rSplit = url.split('&');

    if (typeof rSplit[1] !== 'undefined') {
        $("#tabEventSesRight").removeClass("alert-success");
        $("#tabEventSesRight").removeClass("alert");
        $("#tabEventSesRight").addClass("alert-danger");
        jQuery.validator.unobtrusive.parse('#tabEventSesRight');
    }
}

function SesBgColorClear() {
    $("#tabEventSesRight").removeClass("alert-success");
    $("#tabEventSesRight").removeClass("alert");
    $("#tabEventSesRight").removeClass("alert-danger");
}



/*
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
|                          MANAGEMENT OF EVENT PARTICIPANTS                            |
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
*/

//Update list of Event Participants
function UpdateEventPar(idEv) {
    //alert("IDev: " + idEv);

    $.ajax({
        url: '/EventParticipant/IndexPv',
        dataType: 'html',
        type: 'GET',
        data: { page: 1, idEvent: idEv },
        success: function (data) {    
            $('#tabEventParLeft').html(data);
            jQuery.validator.unobtrusive.parse('#tabEventParRight');
            //Atualizar cor de fundo do painel do lado direito
            ParBgColorClear();
        },
        error: function () {
            alert("ERRO: ao atualizar a lista dos artistas associados ao evento.");
            ParBgColorClear();
        }
    });

}

//Select line the of Event Participants
$('#tablePar tbody tr').on('click', function () {
    //alert("clicou na linha");
    
    var idReg = $(this).attr("id");
    registSelect = 1;

    $(this).closest('table').find('td').removeClass('SelectLineTable');
    $(this).find('td').addClass('SelectLineTable');

    var page = getUrlVars("par")["page"];

    //alert("select session line : " + idReg);

    document.getElementById("btnEditPar").href = "/EventParticipant/EditPv" + "?page=" + page + "&id=" + idReg;
    document.getElementById("btnDelPar").href = "/EventParticipant/DeletePv" + "?page=" + page + "&id=" + idReg;
});

//DoubleClick Action in line the of Event Session
$('#tablePar table.table-striped tbody tr').on('dblclick', function () {
    //alert("double click!");

    var idReg = $(this).attr("id");

    $.ajax({
        url: '/EventParticipant/EditPv',
        dataType: 'html',
        type: 'GET',
        data: { page: 1, id: idReg },
        success: function (data) { 
            $('#tabEventParRight').html(data);
            jQuery.validator.unobtrusive.parse('#tabEventParRight');
            //Atualizar backgroud do painel do lado direito
            ParBgColorEdit();
        },
        error: function () {
            alert("ERRO: ao atualizar a lista dos artistas associados ao evento.");
        }
    });

});

// CHANGE BACKGROUND OF PANEL RIGHT OF PARTICIPANTS            
function ParBgColorAdd() {
    //alert("add...");
        $("#tabEventParRight").removeClass("alert");
        $("#tabEventParRight").removeClass("alert-danger");
        $("#tabEventParRight").addClass("alert-success");
        jQuery.validator.unobtrusive.parse('#tabEventParRight');
}

function ParBgColorEdit() {
    //alert("edit...");
    var url = $('#btnEditPar').attr('href');
    var rSplit = url.split('&');

    if (typeof rSplit[1] !== 'undefined') {
        $("#tabEventParRight").removeClass("alert-success");
        $("#tabEventParRight").removeClass("alert-danger");
        $("#tabEventParRight").addClass("alert");
        jQuery.validator.unobtrusive.parse('#tabEventParRight');
    }
}

function ParBgColorDel() {
    //alert("del...");
    var url = $('#btnEditPar').attr('href');
    var rSplit = url.split('&');

    if (typeof rSplit[1] !== 'undefined') {
        $("#tabEventParRight").removeClass("alert-success");
        $("#tabEventParRight").removeClass("alert");
        $("#tabEventParRight").addClass("alert-danger");
        jQuery.validator.unobtrusive.parse('#tabEventParRight');
    }
}

function ParBgColorClear() {
    $("#tabEventParRight").removeClass("alert-success");
    $("#tabEventParRight").removeClass("alert");
    $("#tabEventParRight").removeClass("alert-danger");
}



/*
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
|                           MANAGEMENT OF EVENT RESOURCES                              |
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
*/

//Update list of Event Resources
function UpdateEventRes(idEv) {
    //alert("IDev: " + idEv);

    $.ajax({
        url: '/Resource/IndexPv',
        dataType: 'html',
        type: 'GET',
        data: { page: 1, idEvent: idEv },
        success: function (data) {
            //jQuery.validator.unobtrusive.parse('#tabEventResRight');
            $('#tabEventResLeft').html(data);
            //Atualizar cor de fundo do painel do lado direito
            ResBgColorClear();
        },
        error: function () {
            alert("ERRO: ao atualizar a lista dos recursos do evento.");
            ResBgColorClear();
        }
    });

}

//Select line the of Event Resources
$('#tableRes tbody tr').on('click', function () {
    //alert("clicou linha registo - CLICK");

    var idReg = $(this).attr("id");
    registSelect = 1;

    $(this).closest('table').find('td').removeClass('SelectLineTable');
    $(this).find('td').addClass('SelectLineTable');

    var page = getUrlVars("res")["page"];

    //alert("select session line : " + idReg);

    document.getElementById("btnEditRes").href = "/Resource/EditPv" + "?page=" + page + "&id=" + idReg;
    document.getElementById("btnDelRes").href = "/Resource/DeletePv" + "?page=" + page + "&id=" + idReg;
});

//DoubleClick Action in line the of Event Resource
$('#tableRes table.table-striped tbody tr').on('dblclick', function () {
    //alert("clicou linha registo - DOUBLE CLICK");

    var idReg = $(this).attr("id");

    $.ajax({
        url: '/Resource/EditPv',
        dataType: 'html',
        type: 'GET',
        data: { page: 1, id: idReg },
        success: function (data) {
            $('#tabEventResRight').html(data);
            jQuery.validator.unobtrusive.parse('#tabEventResRight');
            //Atualizar backgroud do painel do lado direito
            ResBgColorEdit();
        },
        error: function () {
            alert("ERRO: ao atualizar a lista dos recursos do evento.");
        }
    });

});

// CHANGE BACKGROUND OF PANEL RIGHT OF RESOURCES            
function ResBgColorAdd() {
    //alert("add...");
        $("#tabEventResRight").removeClass("alert");
        $("#tabEventResRight").removeClass("alert-danger");
        $("#tabEventResRight").addClass("alert-success");
        jQuery.validator.unobtrusive.parse('#tabEventResRight');
}

function ResBgColorEdit() {
    //alert("edit...");
    var url = $('#btnEditRes').attr('href');
    var rSplit = url.split('&');

    if (typeof rSplit[1] !== 'undefined') {
        $("#tabEventResRight").removeClass("alert-success");
        $("#tabEventResRight").removeClass("alert-danger");
        $("#tabEventResRight").addClass("alert");
        jQuery.validator.unobtrusive.parse('#tabEventResRight');
    }
}

function ResBgColorDel() {
    //alert("del...");
    var url = $('#btnEditRes').attr('href');
    var rSplit = url.split('&');

    if (typeof rSplit[1] !== 'undefined') {
        $("#tabEventResRight").removeClass("alert-success");
        $("#tabEventResRight").removeClass("alert");
        $("#tabEventResRight").addClass("alert-danger");
        jQuery.validator.unobtrusive.parse('#tabEventResRight');
    }
}

function ResBgColorClear() {
    $("#tabEventResRight").removeClass("alert-success");
    $("#tabEventResRight").removeClass("alert");
    $("#tabEventResRight").removeClass("alert-danger");
}


/*
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
|                            MANAGEMENT OF EVENT COMMENTS                              |
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
*/

//Update list of Event Comments
function UpdateEventCom(idEv) {
    //alert("IDev: " + idEv);

    $.ajax({
        url: '/Comment/IndexPv',
        dataType: 'html',
        type: 'GET',
        data: { page: 1, idEvent: idEv },
        success: function (data) {
            $('#tabEventResLeft').html(data);
            //Atualizar cor de fundo do painel do lado direito
           ComBgColorClear();
        },
        error: function () {
            alert("ERRO: ao atualizar a lista dos comentários do evento.");
            ComBgColorClear();
        }
    });

}

//Select line the of Event Comments
$('#tableCom tbody tr').on('click', function () {
    var idReg = $(this).attr("id");
    registSelect = 1;

    $(this).closest('table').find('td').removeClass('SelectLineTable');
    $(this).find('td').addClass('SelectLineTable');

    var page = getUrlVars("ses")["page"];

    //alert("select session line : " + idReg);

    document.getElementById("btnEditCom").href = "/Comment/EditPv" + "?page=" + page + "&id=" + idReg;
    document.getElementById("btnDelCom").href = "/Comment/DeletePv" + "?page=" + page + "&id=" + idReg;
});

//DoubleClick Action in line the of Event Comments
$('#tableCom table.table-striped tbody tr').on('dblclick', function () {
    //alert("double click!");

    var idReg = $(this).attr("id");

    $.ajax({
        url: '/Comment/EditPv',
        dataType: 'html',
        type: 'GET',
        data: { page: 1, id: idReg },
        success: function (data) {
            $('#tabEventComRight').html(data);
            jQuery.validator.unobtrusive.parse('#tabEventComRight');
            ResBgColorEdit();
        },
        error: function () {
            alert("ERRO: ao atualizar a lista dos comentários do evento.");
        }
    });

});

// CHANGE BACKGROUND OF PANEL RIGHT OF COMMENTS            
function ComBgColorAdd() {
    //alert("add...");
    $("#tabEventComRight").removeClass("alert");
    $("#tabEventComRight").removeClass("alert-danger");
    $("#tabEventComRight").addClass("alert-success");
    jQuery.validator.unobtrusive.parse('#tabEventComRight');
}

function ComBgColorEdit() {
    //alert("edit...");
    var url = $('#btnEditCom').attr('href');
    var rSplit = url.split('&');
    alert("rSplit: " + rSplit);

    if (typeof rSplit[1] !== 'undefined') {
        $("#tabEventComRight").removeClass("alert-success");
        $("#tabEventComRight").removeClass("alert-danger");
        $("#tabEventComRight").addClass("alert");
        jQuery.validator.unobtrusive.parse('#tabEventComRight');
    }
}

function ComBgColorDel() {
    //alert("del...");
    var url = $('#btnEditCom').attr('href');
    var rSplit = url.split('&');

    if (typeof rSplit[1] !== 'undefined') {
        $("#tabEventComRight").removeClass("alert-success");
        $("#tabEventComRight").removeClass("alert");
        $("#tabEventComRight").addClass("alert-danger");
        jQuery.validator.unobtrusive.parse('#tabEventComRight');
    }
}

function ComBgColorClear() {
    $("#tabEventComRight").removeClass("alert-success");
    $("#tabEventComRight").removeClass("alert");
    $("#tabEventComRight").removeClass("alert-danger");
}


/*
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
|                         MANAGEMENT OF ACCREDITING REQUEST                            |
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
*/

// SELECT LINE THE LIST OF Acrediting Request                            
$('#tabAccReq tbody tr').on('click', function () {
    //$("table").addClass("table table-hover table-striped table-bordered");
    var idAccReq = $(this).attr("id");
    registSelect = 1;

    $(this).closest('table').find('td').removeClass('SelectLineTable');
    $(this).find('td').addClass('SelectLineTable');

    var urlEditEvent = "/AccreditingRequest/Edit/" + idAccReq;
    var urlDelEvent = "/AccreditingRequest/Delete/" + idAccReq;
    document.getElementById("btnEdit").href = urlEditEvent;
    document.getElementById("btnDel").href = urlDelEvent;


});


// DOUBLE CLICK THE LINE THE LIST OF EVENTS                
$("#tabAccReq tbody").on("dblclick", "tr", function () {
    var idAccReq = $(this).attr("id");
    //$(location).href("/Event/Edit/" + idEvent);
    var url = "/AccreditingRequest/Edit/" + idAccReq;
    $(location).attr('href', url);
});




/*
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
|                           ?????????????????????????????                              |
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
*/





/*
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
|                           ?????????????????????????????                              |
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
*/